package com.ikangtai.shecare.common.baseView;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.text.SpannableString;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ikangtai.shecare.R;
import com.ikangtai.shecare.common.util.PxDxUtil;

public class ListBar extends RelativeLayout {

    private boolean leftBtnVisible;
    private Button mLeftButton;
    private TextView mMidTextView;
    private Button mRightButton;
    private TextView mRightTextView;
    private LinearLayout mBottomLineLinearLayout;
    private static int anyTaskSelected = 0;

    public ListBar(Context context) {
        super(context);
    }

    public ListBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        float density = context.getResources().getDisplayMetrics().density;
        setLeft(context, attrs, density);
        setMiddle(context, attrs, density);
        setRight(context, attrs, density);
        setBottomLine(context, attrs, density);
    }

    private void setLeft(Context context, AttributeSet attrs, float density) {

        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.ListBar);
        leftBtnVisible = ta.getBoolean(R.styleable.ListBar_listBar_leftBtnVisible, true);
        if (!leftBtnVisible) {
            ta.recycle();
            return;
        }

        float leftBtnWidth = (ta.getDimension(R.styleable.ListBar_listBar_leftBtnWidth, 40)) / density;
        float leftBtnHeight = (ta.getDimension(R.styleable.ListBar_listBar_leftBtnHeight, 40)) / density;
        Drawable leftBtnBg = ta.getDrawable(R.styleable.ListBar_listBar_leftBtnBg);

        ta.recycle();

        mLeftButton = new Button(context);
        mLeftButton.setId(R.id.id1);
        mLeftButton.setBackground(leftBtnBg);

        int leftBtnWidthParam = PxDxUtil.dip2px(getContext(), leftBtnWidth);
        int leftBtnHeightParam = PxDxUtil.dip2px(getContext(), leftBtnHeight);
        LayoutParams mLeftParams = new LayoutParams(leftBtnWidthParam, leftBtnHeightParam);
        mLeftParams.leftMargin = (PxDxUtil.dip2px(getContext(), 10));
        mLeftParams.addRule(RelativeLayout.CENTER_VERTICAL);
        addView(mLeftButton, mLeftParams);
    }

    private void setMiddle(Context context, AttributeSet attrs, float density) {
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.ListBar);

        CharSequence midText = ta.getText(R.styleable.ListBar_listBar_midText);
        int midTextColor = ta.getColor(R.styleable.ListBar_listBar_midTextColor, getResources().getColor(R.color.black_overlay));
        float midTextSize = (ta.getDimension(R.styleable.ListBar_listBar_midTextSize, 18)) / density;
        float midMargin = (ta.getDimension(R.styleable.ListBar_listBar_midMargin, 30)) / density;

        ta.recycle();

        mMidTextView = new TextView(context);
        mMidTextView.setId(R.id.id2);
        mMidTextView.setText(midText);
        mMidTextView.setTextColor(midTextColor);
        mMidTextView.setTextSize(midTextSize);


        LayoutParams mMidParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        mMidParams.leftMargin = PxDxUtil.dip2px(getContext(), midMargin);

        if (leftBtnVisible)
            mMidParams.addRule(RelativeLayout.RIGHT_OF, R.id.id1);

        mMidParams.addRule(RelativeLayout.CENTER_VERTICAL);
        addView(mMidTextView, mMidParams);
    }

    private void setRight(Context context, AttributeSet attrs, float density) {
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.ListBar);
        CharSequence rightType = ta.getText(R.styleable.ListBar_listBar_rightType);

        if (rightType.equals("Button"))
            addRightButton(context, ta, density);

        if (rightType.equals("TextView"))
            addRightTextView(context, ta, density);

        if (rightType.equals("default"))
            addDefault(context, ta, density);

        ta.recycle();
    }

    private void addRightButton(Context context, TypedArray ta, float density) {

        Drawable midRightBtnBg = ta.getDrawable(R.styleable.ListBar_listBar_rightBtnBg);
        float rightBtnWidth = (ta.getDimension(R.styleable.ListBar_listBar_rightBtnWidth, 45)) / density;
        float rightBtnHeight = (ta.getDimension(R.styleable.ListBar_listBar_rightBtnHeight, 45)) / density;
        float rightBtnMargin = (ta.getDimension(R.styleable.ListBar_listBar_rightBtnMargin, 30)) / density;

        mRightButton = new Button(context);
        mRightButton.setBackground(midRightBtnBg);

        int rightBtnWidthParam = PxDxUtil.dip2px(getContext(), rightBtnWidth);
        int rightBtnHeightParam = PxDxUtil.dip2px(getContext(), rightBtnHeight);
        LayoutParams mRightButtonParams = new LayoutParams(rightBtnWidthParam, rightBtnHeightParam);
        mRightButtonParams.addRule(RelativeLayout.ALIGN_PARENT_END);
        mRightButtonParams.addRule(RelativeLayout.CENTER_VERTICAL);

        mRightButtonParams.rightMargin = PxDxUtil.dip2px(getContext(), rightBtnMargin);
        addView(mRightButton, mRightButtonParams);
        mRightButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mListener.rightButtonClick(ListBar.this);
            }
        });
    }

    private void addRightTextView(Context context, TypedArray ta, float density) {
        CharSequence rightText = ta.getText(R.styleable.ListBar_listBar_rightText);
        int rightTextColor = ta.getColor(R.styleable.ListBar_listBar_rightTextColor, getResources().getColor(R.color.black_overlay));
        float rightTextSize = (ta.getDimension(R.styleable.ListBar_listBar_rightTextSize, 15)) / density;

        mRightTextView = new TextView(context);
        mRightTextView.setText(rightText);
        mRightTextView.setTextColor(rightTextColor);
        mRightTextView.setTextSize(rightTextSize);
        mRightTextView.setGravity(Gravity.END);

        LayoutParams mRightTvParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        mRightTvParams.addRule(RelativeLayout.RIGHT_OF, R.id.id2);
        mRightTvParams.addRule(RelativeLayout.CENTER_VERTICAL);
        mRightTvParams.addRule(RelativeLayout.ALIGN_PARENT_END);
        mRightTvParams.rightMargin = PxDxUtil.dip2px(getContext(), 15);
        addView(mRightTextView, mRightTvParams);
    }

    private void setBottomLine(Context context, AttributeSet attrs, float density) {
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.ListBar);
        boolean hasBottomLine = ta.getBoolean(R.styleable.ListBar_hasBottomLine, true);
        ta.recycle();

        if (hasBottomLine)
            addBottomLine(context, attrs);
    }

    private void addBottomLine(Context context, AttributeSet attrs) {
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.ListBar);
        int bottomLineColor = ta.getColor(R.styleable.ListBar_bottomLineColor, getResources().getColor(R.color.black_overlay));
        ta.recycle();

        mBottomLineLinearLayout = new LinearLayout(context);
        mBottomLineLinearLayout.setBackgroundColor(bottomLineColor);
        LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, PxDxUtil.dip2px(getContext(), 1));
        lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        addView(mBottomLineLinearLayout, lp);
    }

    public void setBgColor(int color) {
        this.setBackgroundColor(color);
    }

    public void setLeftBtnClicked(boolean leftBtnClicked) {
        mLeftButton.setSelected(leftBtnClicked);
    }


    public void setmMidTextView(SpannableString text) {
        mMidTextView.setText(text);

    }

    public String getmMidTextView() {
        return mMidTextView.getText().toString().trim();
    }

    public void setmRightButton(int text) {
        mRightButton.setBackground(getResources().getDrawable(text));

    }

    public void setmLeftButton(int text) {
        mLeftButton.setBackground(getResources().getDrawable(text));

    }

    public void setRightText(String text) {
        mRightTextView.setText(text);
    }

    public String getRightText() {
        return mRightTextView.getText().toString().trim();
    }

    public interface OnListBarClickListener {
        void rightButtonClick(ListBar listBar);
    }

    public void setOnListBarClickListener(OnListBarClickListener mListener) {
        this.mListener = mListener;
    }

    public void setRightButtonClickAble(boolean isAble) {
        this.mRightButton.setClickable(isAble);
    }

    OnListBarClickListener mListener = new OnListBarClickListener() {
        @Override
        public void rightButtonClick(ListBar listBar) {

        }
    };

    public void setBottomLineColor(int color) {
        this.mBottomLineLinearLayout.setBackgroundColor(color);
    }

    public void setMidClickable(boolean clickable) {
        this.mMidTextView.setClickable(clickable);
    }

    private View.OnClickListener onMidClickListener;

    public void setMidClick(View.OnClickListener onMidClickListener) {
        this.mMidTextView.setOnClickListener(onMidClickListener);
    }

    public void setMidColor(int color) {
        this.mMidTextView.setTextColor(color);
    }

    private void addDefault(Context context, TypedArray ta, float density) {
        Drawable midRightBtnBg = ta.getDrawable(R.styleable.ListBar_listBar_rightBtnBg);
        float rightBtnWidth = (ta.getDimension(R.styleable.ListBar_listBar_rightBtnWidth, 60)) / density;
        float rightBtnHeight = (ta.getDimension(R.styleable.ListBar_listBar_rightBtnHeight, 60)) / density;
        float rightBtnMargin = (ta.getDimension(R.styleable.ListBar_listBar_rightBtnMargin, 30)) / density;

        mRightButton = new Button(context);
        mRightButton.setBackground(midRightBtnBg);

        int rightBtnWidthParam = PxDxUtil.dip2px(getContext(), rightBtnWidth);
        int rightBtnHeightParam = PxDxUtil.dip2px(getContext(), rightBtnHeight);
        LayoutParams mRightButtonParams = new LayoutParams(rightBtnWidthParam, rightBtnHeightParam);
        mRightButtonParams.addRule(RelativeLayout.ALIGN_PARENT_END);
        mRightButtonParams.addRule(RelativeLayout.CENTER_VERTICAL);
        mRightButtonParams.rightMargin = PxDxUtil.dip2px(getContext(), rightBtnMargin);
        addView(mRightButton, mRightButtonParams);
        mRightButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mListener.rightButtonClick(ListBar.this);
            }
        });

        CharSequence rightText = ta.getText(R.styleable.ListBar_listBar_rightText);
        int rightTextColor = ta.getColor(R.styleable.ListBar_listBar_rightTextColor, getResources().getColor(R.color.black_overlay));
        float rightTextSize = (ta.getDimension(R.styleable.ListBar_listBar_rightTextSize, 45)) / density;

        mRightTextView = new TextView(context);
        mRightTextView.setText(rightText);
        mRightTextView.setTextColor(rightTextColor);
        mRightTextView.setTextSize(rightTextSize);
        mRightTextView.setGravity(Gravity.END);

        LayoutParams mRightTvParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        mRightTvParams.addRule(RelativeLayout.RIGHT_OF, R.id.id2);
        mRightTvParams.addRule(RelativeLayout.CENTER_VERTICAL);
        mRightTvParams.addRule(RelativeLayout.ALIGN_PARENT_END);
        mRightTvParams.rightMargin = PxDxUtil.dip2px(getContext(), 10);
        addView(mRightTextView, mRightTvParams);
    }
}
